﻿using App.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace App.WindowsForm
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            FormBorderStyle = FormBorderStyle.None;
            this.Opacity = Config.Opacity;
            InitializeComponent();
            headers.Add("Referer", "http://finance.sina.com.cn");
            this.ShowInTaskbar = false;
        }

        private void Form2_Load(object sender, EventArgs e)
        {

            this.TopMost = true;
            timer1.Interval = 3000;
            timer1.Tick += Timer1_Tick;
            timer1.Start();
        }


        public decimal oldPrice = 0;
        public decimal yestodayPrice = 0;

        public Dictionary<string, string> headers = new Dictionary<string, string>();
        private void Timer1_Tick(object sender, EventArgs e)
        {
            if (!isRun())
                return;

            try
            {
                var code = string.Join(",", Config.Codes);

                var result = HttpHelper.HttpGet($"http://hq.sinajs.cn/list={code}", null, headers);

                var array = result.Split(',');
                var nowPrice = decimal.Parse(array[3].ToString());
                if (oldPrice < nowPrice)
                {
                    label1.ForeColor = Color.Red;
                }
                else if (oldPrice == nowPrice)
                {
                }
                else
                {
                    label1.ForeColor = Color.Green;
                }

                label1.Text = nowPrice.ToString();
                oldPrice = nowPrice;

                yestodayPrice = decimal.Parse(array[2].ToString());
                var upDow = ((nowPrice - yestodayPrice) / yestodayPrice) * 100;
                if (upDow >= 0)
                {
                    label2.Text = "+" + upDow.ToString("0.00") + "%";
                    label2.ForeColor = Color.Red;
                }
                else
                {
                    label2.Text = upDow.ToString("0.00") + "%";
                    label2.ForeColor = Color.Green;
                }

            }
            catch (Exception)
            {

            }
        }

        int oldX, oldY;
        private void mouseD(object sender, MouseEventArgs e)
        {
            //判断是否为鼠标左键
            if (e.Button == MouseButtons.Left)
            {
                //获取鼠标左键按下时的位置
                this.oldX = e.Location.X;
                this.oldY = e.Location.Y;
            }
        }

        private void mouseM(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //计算鼠标移动距离
                this.Left += e.Location.X - this.oldX;
                this.Top += e.Location.Y - this.oldY;
            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
        }

        private bool isRun()
        {
            if (label1.Text == nameof(label1) || label2.Text == nameof(label2))
            {
                return true;
            }

            var timeNow = DateTime.Now;
            var today = DateTime.Now.Date;
            var begin1 = today.AddHours(8).AddMinutes(20);
            var end1 = today.AddHours(11).AddMinutes(32);

            var begin2 = today.AddHours(13).AddMinutes(-2);
            var end2 = today.AddHours(15).AddMinutes(2);

            if (begin1 > timeNow)
                return false;
            if (end1 < timeNow && timeNow < begin2)
                return false;
            if (end2 < timeNow)
                return false;

            if (today.DayOfWeek == DayOfWeek.Sunday
                || today.DayOfWeek == DayOfWeek.Saturday)
                return false;

            return true;
        }
    }
}
